package com.yoomiito.app.ui.video

import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.media.MediaPlayer
import android.os.Bundle
import androidx.constraintlayout.widget.ConstraintLayout
import android.view.MotionEvent
import android.view.View
import android.widget.MediaController
import cn.jzvd.JzvdStd
//import cn.jzvd.JZMediaSystem
//import cn.jzvd.Jzvd
//import cn.jzvd.JzvdStd
//import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.yoomiito.app.Constant
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.model.bean.VideoInfo
import com.yoomiito.app.showToast
import com.yoomiito.app.utils.L
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.act_video_player.*
import java.util.concurrent.TimeUnit

class VideoPlayActivity : BaseActivity<VideoPlayPresenter>() {
    private lateinit var videoInfo: VideoInfo
    private var disposable: Disposable? = null

    companion object {
        fun start(context: Context, url: VideoInfo) {
            val intent = Intent(context, VideoPlayActivity::class.java)
            intent.putExtra(Constant.DATA, url)
            context.startActivity(intent)
        }
    }

    override fun getLayoutId(): Int = R.layout.act_video_player

    override fun newP(): VideoPlayPresenter = VideoPlayPresenter(App.mAppConfig)

    override fun initData(savedInstanceState: Bundle?) {
        videoInfo = intent.getParcelableExtra(Constant.DATA)
        /***********************jiaoziplayer**********************/
        videoPlayer.setUp(videoInfo.url, "", JzvdStd.SCREEN_NORMAL)//JZMediaIjk::class.java
        videoPlayer.startButton?.performClick()
        videoPlayer.backButton.visibility = View.VISIBLE
        videoPlayer.backButton.setOnClickListener {
            finish()
        }
//        back.setOnClickListener { finish() }
//        controlView()
        /******************自带播放器***********************/
//        val mediaController = MediaController(this)
//        val lp = videoPlayer.layoutParams as ConstraintLayout.LayoutParams
//        lp.width = App.mWidthPx
//        lp.height = videoInfo.height.toInt()*App.mWidthPx/videoInfo.width.toInt()
//        videoPlayer.layoutParams = lp
//        videoPlayer.setOnInfoListener { _, what, _ ->
//            if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START){
//                dismissLoading()
//            }
//            true
//        }
//        videoPlayer.setOnErrorListener { mp, what, extra ->
//            "播放失败".showToast()
//            finish()
//            return@setOnErrorListener true
//        }
//        videoPlayer.setMediaController(mediaController)
//        videoPlayer.setVideoPath(videoInfo.url)
//        videoPlayer.start()
//        showLoading()
//        back.setOnClickListener { finish() }
//        controlView()
        /**********************GSYVideoManager***************************/
//        videoPlayer.backButton.visibility = View.VISIBLE
//        videoPlayer.isAutoFullWithSize = true
//        videoPlayer.setUp(videoUrl,true,null)
//        videoPlayer.backButton.setOnClickListener {
//            onBackPressed()
//        }
//        videoPlayer.startButton.performClick()

        /**************************jiaoziplayer**********************************/
//        videoPlayer.setUp(videoUrl,null,Jzvd.SCREEN_FULLSCREEN, JZMediaIjk::class.java)
//        videoPlayer.setUp(videoUrl,null)
//        videoPlayer.startVideo()
    }

//    override fun onTouchEvent(event: MotionEvent?): Boolean {
//        val action = event?.action
//        if (MotionEvent.ACTION_DOWN == action) {
//            if (View.INVISIBLE == videoPlayTitleView.visibility) {
//                videoPlayTitleView.visibility = View.VISIBLE
//            }
//            controlView()
//        }
//
//        return super.onTouchEvent(event)
//    }

//    private fun controlView(){
//        if (disposable!=null&&!disposable?.isDisposed!!){
//            disposable?.dispose()
//        }
//        disposable = null
//        disposable = Observable.interval(0, 1, TimeUnit.SECONDS)
//                .compose(bindToLifecycle())
//                .take(4)
//                .observeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .doOnComplete {
//                    videoPlayTitleView.visibility = View.INVISIBLE
//                }
//                .subscribe()
//    }

//    override fun onBackPressed() {
////        videoPlayer.setVideoAllCallBack(null)
////        GSYVideoManager.releaseAllVideos()
////        if (Jzvd.backPress()) {
////            return
////        }
//        finish()
//        super.onBackPressed()
//    }

//    override fun attachBaseContext(newBase: Context?) {
//        super.attachBaseContext( ContextWrapper(newBase))
//    }
//
//    override fun getSystemService(name: String): Any? {
//        if (Context.AUDIO_SERVICE == name)
//            return applicationContext.getSystemService(name)
//        return super.getSystemService(name)
//    }

    override fun onResume() {
        super.onResume()
//        videoPlayer.onVideoResume()
//        Jzvd.goOnPlayOnResume()
    }

    override fun onPause() {
        super.onPause()
//        videoPlayer.onVideoPause()
//        Jzvd.releaseAllVideos()

    }

    override fun onDestroy() {
        JzvdStd.releaseAllVideos()
//        disposable?.dispose()
//        disposable = null
        super.onDestroy()
    }

}